#!/bin/sh
unset LIBPATH
#
#   Copyright (c) International Business Machines  Corp., 2000
#
#   This program is free software;  you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY;  without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
#   the GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program;  if not, write to the Free Software
#   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
#
#
#  FILE   : echo
#
#  PURPOSE: Stresses the inetd/xinetd daemon using the `echo` service.
#
#  SETUP: The echo service MUST be active on the RHOST machine. This is
#         controlled by the inetd/xinetd daemon.
#
#  HISTORY:
#    03/01 Robbie Williamson (robbiew@us.ibm.com)
#      -Ported
#
#
#***********************************************************************

#-----------------------------------------------------------------------
#
# FUNCTION:  do_setup
#
#-----------------------------------------------------------------------

do_setup()
{

    NUMLOOPS=${NUMLOOPS:-2}
    TST_TOTAL=$NUMLOOPS

    export TST_COUNT TST_TOTAL

    exists hostname sum
    tst_setup

    TC=sine
    RHOST=${RHOST:-`hostname`}
    CLEANUP=${CLEANUP:-ON}
    CREATEFILE=createfile
    ECHOES=echoes${EXEC_SUFFIX}

    ECHOFILE=${ECHOFILE:-echofile${EXEC_SUFFIX}}
    ECHOSIZE=${ECHOSIZE:-8192}
    NUMPROCESSES=${NUMPROCESSES:-10}

}

#-----------------------------------------------------------------------
#
# FUNCTION:  do_test
# PURPOSE:   Loop until interrupted creating the echo file and then
#	     echoing it to RHOST.
# INPUT:     None.
# OUTPUT:    Informational messages are logged into the run log.
#
#-----------------------------------------------------------------------

do_test()
{
    echo  "Creating echo file $ECHOFILE"
    if ! "$LTPROOT/testcases/bin/$CREATEFILE" $ECHOSIZE "$TCtmp/$ECHOFILE"; then
        end_testcase "$ECHOFILE not created"
    fi

    echo "Compute the checksum of this file"
    csum1=$(sum $TCtmp/$ECHOFILE | awk '{print $1}')
    [ $csum1 ] || end_testcase "initial checksum computation failed"

    while [ $TST_COUNT -le $NUMLOOPS ]; do

        if ! $ECHOES $RHOST "$TCtmp/$ECHOFILE" $NUMPROCESSES; then
            end_testcase "Error in $ECHOES test in loop $TST_COUNT"
        fi

        NUM=0
        while [ $NUM -lt $NUMPROCESSES ]
        do
            csum2=$(sum "$TCtmp/${ECHOFILE}${NUM}" | awk '{print $1}')
            if [ "$csum1" != "$csum2" ]; then
                end_testcase "Checksums differ in loop $TST_COUNT"
            fi
            NUM=$(( $NUM + 1 ))
        done

        tst_resm TINFO "Execution $TST_COUNT passed."

        incr_tst_count
        if [ $TST_COUNT -le $NUMLOOPS ]; then
            tst_resm TINFO "Sleeping 60 seconds to avoid hitting max. connections setting for service"
            sleep 60
        fi
    done
}

#-----------------------------------------------------------------------
#
# FUNCTION:  MAIN
# PURPOSE:   To invoke functions that perform the tasks as described in
#	     the design in the prolog above.
# INPUT:     See SETUP in the prolog above.
# OUTPUT:    Logged run results written to testcase run log
#
#-----------------------------------------------------------------------

. net_cmdlib.sh

read_opts $*
do_setup
do_test
end_testcase
